package com.common.autocode.xml;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.PrintWriter;

import nu.xom.Attribute;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Elements;
import nu.xom.Node;
import nu.xom.Nodes;
import nu.xom.ParentNode;
import nu.xom.XPathContext;
import nux.xom.pool.BuilderFactory;
import nux.xom.xquery.XQueryUtil;


public class XmlUtils {

	public static void modifySpringBeanXml(String packagename){
		try {
			//扫描MyBaits配置
			//<value>classpath:com/temp/**/*Mapper.xml</value>
			String filename = "src/config/spring/beans.xml";
			File file = new File(filename);
			Document document = document = new BuilderFactory().createBuilder(false)
					.build(file);
			Element root = document.getRootElement();
			
			Node node = XQueryUtil.xquery(document, "//*:property[@name='mapperLocations']").get(0);
			Element listroot = (Element) node.getChild(1);
			Elements childs = listroot.getChildElements();
			boolean isContains = false;
			String mapper_path = "classpath:"+packagename.replaceAll("\\.", "/")+"/**/*Mapper.xml";
			for(int i=0;i<childs.size();i++){
				Element ele = childs.get(i);
				if(ele.getValue().contains(mapper_path)){
					isContains = true;
				}
			}
			if(!isContains){
				Element add = new Element("value");
				add.appendChild(mapper_path);
				listroot.appendChild(add);
			}
			
			//<property name="basePackage" value="com.manage.infoboard,com.temp" />
			Element scanner_mapper = (Element) XQueryUtil.xquery(document, "//*:bean[@class='org.mybatis.spring.mapper.MapperScannerConfigurer']/*[@name='basePackage']").get(0);
			Attribute scanner_mapper_attr = scanner_mapper.getAttribute("value");
			String scanner_mapper_oldvalue = scanner_mapper_attr.getValue();
			if(!scanner_mapper_oldvalue.contains(packagename)){
				String scanner_mapper_value = scanner_mapper_oldvalue+","+packagename;
				scanner_mapper_attr.setValue(scanner_mapper_value);
			}
			writerFile(filename,document.toXML());
			
			//扫描Controller
			filename = "src/config/spring/beans-controller.xml";
			file = new File(filename);
			document = document = new BuilderFactory().createBuilder(false)
					.build(file);
			Attribute attrNode = (Attribute) XQueryUtil.xquery(document, "//@base-package").get(0);
			String oldvalue = attrNode.getValue();
			String addvalue = packagename+".*";
			if(!oldvalue.contains(addvalue)){
				String attrValue = oldvalue+","+addvalue;
				attrNode.setValue(attrValue);
			}
			writerFile(filename,document.toXML());
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static void writerTilesXml(String packagename,String classname){
		String filename = "src/config/tiles/tiles-"+classname+".xml";
		StringBuffer start = new StringBuffer();
		StringBuffer end = new StringBuffer("</tiles-definitions>");
		StringBuffer buffer = new StringBuffer();
		start.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		start.append("<!DOCTYPE tiles-definitions PUBLIC \"-//Apache Software Foundation//DTD Tiles Configuration 3.0//EN\" \"http://tiles.apache.org/dtds/tiles-config_3_0.dtd\" >");
		start.append("<tiles-definitions>");
		buffer.append("<definition name=\"list_"+classname+"_view\" extends=\".base\" >");
		buffer.append("<put-attribute name=\"center\" value=\"/WEB-INF/pages/"+packagename.replaceAll("\\.", "/")+"/"+classname+"_list.jsp\"></put-attribute>");
		buffer.append("</definition>");
		
		buffer.append("<definition name=\"view_"+classname+"_view\" extends=\".view.center\" >");
		buffer.append("<put-attribute name=\"center\" value=\"/WEB-INF/pages/"+packagename.replaceAll("\\.", "/")+"/"+classname+"_view.jsp\"></put-attribute>");
		buffer.append("</definition>");
		
		buffer.append("<definition name=\"edit_"+classname+"_view\" extends=\".view.center\" >");
		buffer.append("<put-attribute name=\"center\" value=\"/WEB-INF/pages/"+packagename.replaceAll("\\.", "/")+"/"+classname+"_edit.jsp\"></put-attribute>");
		buffer.append("</definition>");
		
		start.append(buffer);
		start.append(end);
		writerFile(filename,start.toString());
	}
	
	private static void writerFile(String filename,String document) {
		File file = new File(filename);
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(file);
			pw.write(document);
			pw.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}finally{
			if(pw != null)
				pw.close();
		}
	}
	public static void main(String[] args) {
		modifySpringBeanXml("com.a.temp");
	}
}
